package com.zjcode.cdcer.service.impl;

import com.chinatechstar.component.commons.entity.MPage;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.service.BaseService;
import com.chinatechstar.component.commons.supports.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zjcode.cdcer.mapper.CerGastroscopeDiagnosisCollectInfoMapper;
import com.zjcode.cdcer.mapper.TkmGastroscopeDiagnosisInfoMapper;
import com.zjcode.cdcer.model.TkmGastroscopeDiagnosisInfo;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.CerGastroscopeDiagnosisCollectInfoService;
import com.zjcode.cdcer.service.SysUserService;
import com.zjcode.cdcer.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class CerGastroscopeDiagnosisCollectInfoServiceImpl implements CerGastroscopeDiagnosisCollectInfoService, BaseService {

    @Autowired
    private CerGastroscopeDiagnosisCollectInfoMapper cerGastroscopeDiagnosisCollectInfoMapper;
    @Autowired
    private TkmGastroscopeDiagnosisInfoMapper tkmGastroscopeDiagnosisInfoMapper;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public Map<String, Object> listPageData(PageData pageData) {

        String userId = (String) pageData.get("user");

        String extFlag = sysUserService.findUserExtFlag(userId);
        pageData.put("extFlag", extFlag);

        MPage mPage = getPageInfo(pageData);
        Page<Object> page = PageHelper.startPage(mPage.getCurrentPage(), mPage.getPageSize());

        List<LinkedHashMap<String,Object>> maps = cerGastroscopeDiagnosisCollectInfoMapper.listPageData(pageData);

        return PaginationBuilder.buildResult(maps,
                page.getTotal(),
                mPage.getCurrentPage(),
                mPage.getPageSize());
    }

    @Override
    public Map<String, Object> findByOrgIdAndUploadYear(PageData pageData) {
        //拿到单条统计信息详情
        List<TkmGastroscopeDiagnosisInfo> info =
                tkmGastroscopeDiagnosisInfoMapper.findByOrgIdAndUploadYear(pageData);
        //得到单条统计信息
        LinkedHashMap<String,Object> obj =
                cerGastroscopeDiagnosisCollectInfoMapper.findByOrgIdAndUploadYear(pageData);
        //拿到处理完整的数据格式返回
        return Utils.saveDiagnosisInfo(info, obj);
    }

    @Override
    public JsonObj changStatus(PageData pageData) {
        tkmGastroscopeDiagnosisInfoMapper.changStatus(pageData);
        return JsonObj.success("审核成功！");
    }
}
